<?php

namespace app\modules\m\controllers;

use yii\web\Controller;
use \app\modules\m\controllers\common\BaseController;
use \app\common\services\captcha\ValidateCode;
use \app\common\services\UtilService;

use \app\models\sms\SmsCaptcha;
use \app\models\brand\BrandImages;
use \app\models\brand\BrandSetting;

class DefaultController extends BaseController{
    //设置图形验证码的key值
	private $captcha_cookie_name="validate_code";
    public function actionIndex()
    {
    	$info=BrandImages::find()->orderBy(['id'=>SORT_DESC])->all();
    	$one=BrandSetting::find()->one();
        return $this->render('index',['info'=>$info,'one'=>$one]);
    }
    //验证码
    public function actionImg_captcha(){
        $font_path=\Yii::$app->getBasePath()."/web/font-awesome/fonts/captcha.ttf";
        $captcha_handle=new ValidateCode($font_path);
        $captcha_handle->doimg();
        // echo $captcha_handle->getCode();
    	$this->setCookie($this->captcha_cookie_name,$captcha_handle->getCode());
        

    }
    //获取手机验证码
    public function actionGet_captcha(){
       
        $mobile=$this->post("mobile","");
        $img_captcha=$this->post("img_captcha","");
        
        $captcha_code=$this->getCookie($this->captcha_cookie_name);
       

        if($img_captcha !=strtolower($captcha_code)){
           
            return $this->renderJson([],"图形验证码输入不正确,你输入的是$img_captcha,正确的是$captcha_code",-1);
        }
       
        $new_smcaptcha=new SmsCaptcha();
        $new_smcaptcha->getCustomCaptcha($mobile,UtilService::getIp());
        
        if($new_smcaptcha){
            return $this->renderJson([],"发送成功,手机验证码是".
                $new_smcaptcha->captcha);
        }
         return $this->renderJson([],"系统繁忙,稍后再试",-1);
    }
}
